//app.js
var net = require('utils/network.js')
App({
  onLaunch: function () {
    //调用API从本地缓存中获取数据
    // var logs = wx.getStorageSync('logs') || []
    var self = this;
    // logs.unshift(Date.now())
      
    console.log(wx);
    wx.login({
      success: function(res) {
        if (res.code) {
          //发起网络请求
          wx.request({
            url: 'https://api.weixin.qq.com/sns/jscode2session?appid=wx40605d18d836bc2d&secret=e45aafe277598c570ce46255ed62b793&js_code='+res.code+'&grant_type=authorization_code',
            success:function(res){
              console.log(res);
              self.globalData.openid = res.data.openid;
              self.globalData.session_key = res.data.session_key;
              wx.getUserInfo({
                withCredentials:false,
                success: function(res) {
                  var userInfo = res.userInfo
                  var nickName = userInfo.nickName
                  var avatarUrl = userInfo.avatarUrl
                  var gender = userInfo.gender //性别 0：未知、1：男、2：女
                  var province = userInfo.province
                  var city = userInfo.city
                  var country = userInfo.country
                  self.globalData.nickName= nickName;
                  self.globalData.avatarUrl= avatarUrl;
                  self.globalData.gender= gender;
                }
              })
            },fail:function(res){
              console.log("login fail",res);
            },complete(res){

            }
          })
        } else {
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      }
    });
    // wx.setStorageSync('logs', logs)
    wx.getSystemInfo({ 
      success: function (res) { 
        console.log(res)
        var windowWidth = res.screenWidth; 
        var windowHeight = res.screenHeight; 
        self.globalData.screenW = windowWidth
        self.globalData.screenH = windowHeight
        self.globalData.isIOS = res.platform=="ios"?true:false;
      }
    })
  },
  request({url, data, success, fail, complete, method}){
    net({url, data, success, fail,complete, method})
  },
  globalData:{
    wwxx:wx,
    openid:'',
    session_key:'',
    nickName:'',
    gender:'',
    avatarUrl:'',
    isIOS:false,
    userInfo:null,
    screenW:320,
    screenH:568,
    linkstate:'连接心率带',
    linked:false,
    deviceId:'',
    deviceList:[],
    topHeart:0,
    UserData:{
      id: 22,
        age: 25,
        sex: 1,
        mobile: "15202373874",
        icon: "www.baidu.com",
        zan: 0,
        max_heart: 195,
        qr_code: null,
        nick_name: "占",
        name: null,
        open_id: "sdsc134sd6",
        create_time: "2017-04-25 16:42:40",
        update_time: "2017-04-25 16:42:40",
        psw: "e10adc3949ba59abbe56e057f20f883e",
        deviceId:''
    }
  }
})